<?php

namespace app\common;

use think\facade\Config;
require_once '../vendor/ocr/AipOcr.php';
class Ai
{
    /**
     * phpmailer对象
     */
    protected $client;

    /**
     * 构造函数
     */
    public function __construct()
    {
        $config = Config::get('ocr');
        $this->client = new \AipOcr($config['ai_app_id'],$config['ai_api_key'],$config['ai_secret_key']);
    }

    static private function _check_file_path_type_is_url($file_path) {
        if (preg_match('/http/', $file_path)) {
            return true;
        }
        return false;
    }

    /**
     * 身份证识别
     * @param $file_path //图片地址
     * @param $front //正反
     */
    public function getIdCard($file_path,$front) {
        if (self::_check_file_path_type_is_url($file_path)) {
            return $this->client->idcard($file_path);
        } else {
            $image = file_get_contents($file_path);
            return $this->client->idcard($image,$front);
        }
    }


    public function request_post($url = '', $param = '') {
        if (empty($url) || empty($param)) {
            return false;
        }
//        var_dump($param);exit;
        $postUrl = $url;
        $curlPost = $param;
        $curl = curl_init();//初始化curl
        curl_setopt($curl, CURLOPT_URL,$postUrl);//抓取指定网页
        curl_setopt($curl, CURLOPT_HEADER, 0);//设置header
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
        curl_setopt($curl, CURLOPT_POST, 1);//post提交方式
        curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost);
        $data = curl_exec($curl);//运行curl
        curl_close($curl);
        return $data;
    }

    
}